package com.dx_society.controller;

import com.dx_society.dto.Result;
import com.dx_society.service.ActivitiesService;
import com.dx_society.service.SocietiesService;
import com.dx_society.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private ActivitiesService activitiesService;
    @Resource
    private UserService userService;
    @Resource
    private SocietiesService societiesService;

    /**
     * 查询活动列表
     *
     * @param state 0:待审核 1:已通过 2:含已过期活动
     * @return 活动列表
     */
    @GetMapping(value = "/activity/list")
    public Result queryActivityList(@RequestParam("state") Integer state) {
        if (state == 0) {
            return activitiesService.queryUnpubAc();
        } else if (state == 1) {
            return activitiesService.queryActivitiesList(null, null, 2, true);
        } else if (state == 2) {
            return activitiesService.queryActivitiesList(null, null, 2, false);
        }
        return null;
    }

    /**
     * 审批活动
     * @param agree true通过 false拒绝
     * @return 成功与否
     */
    @PostMapping(value = "/activity/agree")
    public Result agreeActivity(@RequestParam(value = "agree") Boolean agree,
                                @RequestParam("ac_id")Long acId) {
        return activitiesService.agreeActivity(agree,acId);
    }

    /**
     * 删除活动
     * @param acId 活动id
     * @return 成功与否
     */
    @PostMapping(value = "/activity/delete")
    public Result deleteActivity(@RequestParam("ac_id")Long acId){
        if(activitiesService.removeById(acId)){
            return Result.ok();
        }else {
            return Result.fail("删除失败");
        }
    }

    /**
     * 获取页面uv
     *
     * @param date 日期 格式为 yyyy-MM-dd
     * @return uv
     */
    @GetMapping("/uv")
    public Result getUV(@RequestParam("date") String date){
        return userService.getUV(date);
    }

    /**
     * 获取用户列表
     * @return 用户列表
     */
    @GetMapping("/user/list")
    public Result getUserList(){
        return Result.ok(userService.list());
    }
    /**
     * 删除用户
     * @param userId 用户id
     * @return 成功与否
     */
    @PostMapping("/user/delete")
    public Result deleteUser(@RequestParam("user_id") Long userId){
        if(userService.removeById(userId)){
            return Result.ok();
        }else {
            return Result.fail("删除失败");
        }
    }

    /**
     * 获取社团列表
     * @return 社团列表
     */
    @GetMapping("/society/list")
    public Result getSocietyList(){
        return Result.ok(societiesService.list());
    }

    /**
     * 删除社团
     * @param soId 社团id
     * @return 成功与否
     */
    @PostMapping("/society/delete")
    public Result deleteSociety(@RequestParam("so_id") Long soId){
        if(societiesService.removeById(soId)){
            return Result.ok();
        }else {
            return Result.fail("删除失败");
        }
    }



}
